import Item_cart_li.ItemsWindow;
import pojo.Businessman;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;


class ImageTextPanel extends JPanel {
    private ImageIcon imageIcon;
    private String text;
    private String id; // 唯一标识符
    private JPanel parentPanel;

    private JFrame detailFrame = null;
    private Businessman businessman;

    public String getId(){
        return id;
    }

    public String getText() {
        return text;
    }

    public ImageTextPanel(String id, ImageIcon imageIcon, String text, JPanel parentPanel) {
        this.id = id;
        this.imageIcon = imageIcon;
        this.text = text;
        this.parentPanel = parentPanel;
        setLayout(new BorderLayout());

        // 创建文本区域
        JTextArea textArea = new JTextArea(text);
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setBackground(new Color(223,225,229));
        textArea.setWrapStyleWord(true);
        textArea.setFont(new Font("Arial", Font.PLAIN, 14));
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane, BorderLayout.CENTER);

        // 创建图片区域
        JLabel imageLabel = new JLabel();
        imageLabel.setHorizontalAlignment(JLabel.CENTER);
        int panelWidth = 450;
        int newWidth = panelWidth / 3;
        double aspectRatio = (double) imageIcon.getIconWidth() / imageIcon.getIconHeight();
        int newHeight = (int) (newWidth / aspectRatio);
        imageLabel.setIcon(new ImageIcon(imageIcon.getImage().getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH)));
        add(imageLabel, BorderLayout.WEST);

        // 鼠标点击事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                openPanel(id);  // 触发打开新面板
            }
        });
    //    this.setBorder(BorderFactory.createLineBorder(Color.black,2));
        this.setFocusable(true);  // 确保面板可响应鼠标事件
        this.setRequestFocusEnabled(true);  // 确保面板可以请求焦点
    }



    public void openPanel(String id) {
       /* JFrame detailFrame = new JFrame("Detail of " + id);
        detailFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        detailFrame.setSize(400, 300);

        JPanel detailPanel = new JPanel(new BorderLayout());
        JLabel imageLabel = new JLabel(new ImageIcon(imageIcon.getImage().getScaledInstance(200, 200, Image.SCALE_SMOOTH)));
        JTextArea textArea = new JTextArea(text);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        detailPanel.add(imageLabel, BorderLayout.NORTH);
        detailPanel.add(scrollPane, BorderLayout.CENTER);

        detailFrame.add(detailPanel);
        detailFrame.setVisible(true);
        */

        if (detailFrame != null) {
            detailFrame.dispose();  // 关闭原窗口
            detailFrame = null;  // 清空引用
        }

        businessman = new Businessman(Integer.parseInt(id));
        detailFrame = new JFrame();
        detailFrame.setTitle(businessman.getName());
        detailFrame.setIconImage(businessman.getImageIcon());
        detailFrame.setSize(new Dimension(450, 700));
        detailFrame.setResizable(false); // 禁止调整大小
        // 创建ItemsWindow并将其作为内容面板

        ItemsWindow window = new ItemsWindow(Integer.parseInt(id));
        detailFrame.setContentPane(window.getItem_window());
        // 设置关闭窗口时不退出整个应用程序
        detailFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // 显示窗口
        detailFrame.setVisible(true);
    }
}